package com.tbit.main.controller;

import cn.hutool.core.collection.ListUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.pojo.VO.MachineInfoVO;
import com.tbit.main.pojo.VO.MachineVO;
import com.tbit.main.service.CalibrateService;
import com.tbit.main.service.MachineFunctionService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

/**
 * 车辆管理
 *
 * @author LMY
 * @create 2021-05-11 19:20
 */
@RestController
@RequestMapping("/machineCon")
public class MachineConController {

    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private MachineFunctionService machineFunctionService;

    /**
     * 根据车辆编号查询
     *
     * @param token
     * @param userCode 车辆编号
     * @param mapType  地图类型
     * @param machineStatusType 设备状态类型 0:闲置 1:骑行中 2:运维占用 3:异常
     * @throws ParseException
     */
    @RequestMapping("/getByUserCode")
    public JsonResult<MachineInfoVO> getByUserCode(@NotNull String token, @NotNull String userCode,
                                                 @NotNull Integer mapType, @NotNull Integer machineStatusType) throws ParseException {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);

        /**查询设备信息*/
        MachineVO machineVO = machineService.getByUseCodeAndMachineStatus(userCode, machineStatusType);
        if (machineVO == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machineVO.getMachineId());

        MachineInfo machineInfoDb = machineService.getMachineInfo(machineVO.getMachineId());

        /**校准经纬度*/
        if (machineInfoDb.getMachineStatus() != null) {
            Point point = new Point(machineInfoDb.getMachineStatus().getLon(), machineInfoDb.getMachineStatus().getLat());
            calibrateService.commonCalibrate(point, MapConstant.MAP_ORI, mapType);
            machineInfoDb.getMachineStatus().setLonC(point.getX());
            machineInfoDb.getMachineStatus().setLatC(point.getY());
        }
        MachineInfoVO machineInfo = new MachineInfoVO();
        BeanUtils.copyProperties(machineInfoDb, machineInfo);
        machineInfo.setMachine(machineVO);
        //查询车辆功能详情
        List<MachineFunction> machineFunctions = machineFunctionService.getMachineIdsType(ListUtil.of(machineVO.getMachineId()), null);
        machineInfo.getMachine().setMachineFunctionList(machineFunctions);
        return JsonResult.succ(machineInfo);

    }

    /**
     * 根据区域id查询
     *
     * @param accountId    区域id
     * @param rowCount     行数
     * @param pageNO       页数
     * @param machineType  车辆类型
     * @param functionType 设备功能类型 头盔锁1;垂直停车2;高精度定位3;脚撑4;道钉5;rfid6;摄像头7;仪表盘8;文明骑行摄像头9;
     * @param mapType 地图类型
     * @param machineStatusType 设备状态类型 0:闲置 1:骑行中 2:运维占用 3:异常
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult<List<MachineVO>> getByAccountId(@NotNull Integer accountId, Integer rowCount, Integer pageNO,
                                                      @RequestParam(value = "machineType", required = false) Integer machineType,
                                                      @RequestParam(value = "functionType", required = false) Integer functionType,
                                                      @RequestParam(value = "mapType", required = false,defaultValue = "2") Integer mapType,
                                                      @RequestParam(value = "machineStatusType", required = false) Integer machineStatusType) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        Integer accountCount = machineService.getCountByAccountIdType(accountId, machineType, functionType, machineStatusType);
        List<MachineVO> machineList = new LinkedList<>();
        if (accountCount > 0) {
            machineList = machineService.getByAccountIdPag(accountId, machineType, pageNO, rowCount, functionType, mapType, machineStatusType);
        }

        return JsonResult.succ(accountCount, machineList);
    }
}

